package com.example.courseapp.data

import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import android.util.Log
import com.example.courseapp.data.model.LoggedInUser
import com.example.courseapp.data.model.UserInfo
import java.io.IOException
import java.lang.StrictMath.random


/**
 * Class that handles authentication w/ login credentials and retrieves user information.
 */
class LoginDataSource {

    var mondayList:ArrayList<String> = ArrayList<String>()
    var tuesdayList:ArrayList<String> = ArrayList<String>()
    var wednesdayList:ArrayList<String> = ArrayList<String>()
    var thursdayList:ArrayList<String> = ArrayList<String>()
    var fridayList:ArrayList<String> = ArrayList<String>()
    var saturdayList:ArrayList<String> = ArrayList<String>()
    var sundayList:ArrayList<String> = ArrayList<String>()

    var courseTimeMap:HashMap<Int, String> = HashMap<Int, String>()

    var weekMap:HashMap<Int, String> = HashMap<Int, String>() //define week hashmap
    var courseMap:HashMap<Int, ArrayList<String>> = HashMap<Int,ArrayList<String>>() //define course hashmap
    var userMap:HashMap<String,String> = HashMap<String,String>() //define course hashmap
    val dataPreferences= DataPreferences()

    fun login(context: Context,username: String, password: String): Result<LoggedInUser> {
        try {
            // TODO: handle loggedInUser authentication
            // 验证登录名与密码
            if(userMap.get(username)==password){
                val loginUser = LoggedInUser(java.util.UUID.randomUUID().toString(), username)
                // 存储数据
                dataPreferences.saveUserInfo(context,username,password)
                //Log.d("data context指向",context.toString())
                //Log.d("data context用户名takename",dataPreferences.takeName(context).toString())
                //Log.d("data context用户名getUserinfo",dataPreferences.getUserInfo(context).toString())
                return Result.Success(loginUser)
            }else{
                return Result.Error(IOException("Error password"))
            }
        } catch (e: Throwable) {
            return Result.Error(IOException("Error logging in", e))
        }
    }

    fun logout(context:Context) {
        // TODO: revoke authentication
        dataPreferences.clear(context)
        //清除登录数据
    }

    fun initData(){
        // 每日课程绑定
        mondayList= arrayListOf<String>("java","c++","","","")
        tuesdayList= arrayListOf<String>("java","","c++","","")
        wednesdayList= arrayListOf<String>("需求工程","c++","","","软件测试")
        thursdayList= arrayListOf<String>("java","c++","","","")
        fridayList= arrayListOf<String>("","kotlin","","java","")
        saturdayList= arrayListOf<String>("","","","","")
        sundayList= arrayListOf<String>("","","","","")

        // 星期数据绑定
        weekMap.put(1, "Monday")
        weekMap.put(2, "Tuesday")
        weekMap.put(3, "Wednesday")
        weekMap.put(4, "Thursday")
        weekMap.put(5, "Friday")


        courseTimeMap.put(1,"8:00~9:35")
        courseTimeMap.put(2,"9:55~12:20")
        courseTimeMap.put(3,"14:00~15:35")
        courseTimeMap.put(4,"15:55~16:40")
        courseTimeMap.put(5,"18:40~21:05")



        // 绑定课程数据
        courseMap.put(1, mondayList)
        courseMap.put(2, tuesdayList)
        courseMap.put(3, wednesdayList)
        courseMap.put(4, thursdayList)
        courseMap.put(5, fridayList)
        courseMap.put(6, saturdayList)
        courseMap.put(7, sundayList)

        //绑定用户数据
        userMap.put("陈勇劲","123456")
        userMap.put("麦振杰","123456")
        userMap.put("王洪","123456")
        userMap.put("孙玉欣","123456")
    }


}